package com.anyou.mall.dri.service.impl;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.exception.ServiceException;
import com.anyou.common.utils.DateUtils;
import com.anyou.common.utils.StringUtils;
import com.anyou.mall.dri.domain.Driver;
import com.anyou.mall.dri.mapper.DriverMapper;
import com.anyou.mall.dri.param.EnableParam;
import com.anyou.mall.dri.service.IDriverService;
import com.anyou.mall.dri.vo.DriverDetailsVo;
import com.anyou.mall.dri.vo.DriverOrderNumVo;
import com.anyou.mall.dri.vo.DriverOrderVo;
import com.anyou.mall.order.domain.OrdOrder;
import com.anyou.mall.order.service.IOrdOrderService;
import com.anyou.mall.scancode.ScanCodeParam;
import com.anyou.mall.scancode.ScanCodeUtil;
import com.anyou.mall.util.AssertUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 司机列表Service业务层处理
 *
 * @author gaochaohao
 * @date 2022-04-02
 */
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper,Driver> implements IDriverService
{
    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private IOrdOrderService orderService;

    /**
     * 查询司机列表
     *
     * @param driverId 司机列表主键
     * @return 司机列表
     */
    @Override
    public Driver selectDriverByDriverId(Long driverId)
    {
        return driverMapper.selectDriverByDriverId(driverId);
    }

    /**
     * 查询司机列表列表
     *
     * @param driver 司机列表
     * @return 司机列表
     */
    @Override
    public List<Driver> selectDriverList(Driver driver)
    {
        return driverMapper.selectDriverList(driver);
    }

    /**
     * 新增司机列表
     *
     * @param driver 司机列表
     * @return 结果
     */
    @Override
    public int insertDriver(Driver driver)
    {
        //查询司机表中是否有相同账号的用户
        Driver dri = driverMapper.selectOne(new LambdaQueryWrapper<Driver>()
                .eq(Driver::getAccount,driver.getAccount())
                .eq(Driver::getDelFlag,"0"));
        if(dri!=null){
            throw new ServiceException("已存在与当前账号相同的司机，请换个账号!");
        }

        driver.setCreateTime(DateUtils.getNowDate());
        return driverMapper.insert(driver);
    }

    /**
     * 修改司机列表
     *
     * @param driver 司机列表
     * @return 结果
     */
    @Override
    public int updateDriver(Driver driver)
    {
        driver.setUpdateTime(DateUtils.getNowDate());
        if(StringUtils.isNotEmpty(driver.getAccount())){
            driver.setIndividualAuditPhone(driver.getAccount());
        }
        return driverMapper.updateById(driver);
    }

    /**
     * 批量删除司机列表
     *
     * @param driverIds 需要删除的司机列表主键
     * @return 结果
     */
    @Override
    public int deleteDriverByDriverIds(Long[] driverIds)
    {
        return driverMapper.deleteDriverByDriverIds(driverIds);
    }

    /**
     * 删除司机列表信息
     *
     * @param driverId 司机列表主键
     * @return 结果
     */
    @Override
    public int deleteDriverByDriverId(Long driverId)
    {
        return driverMapper.deleteDriverByDriverId(driverId);
    }

    @Override
    public ApiResult enableStatus(Long driverId) {
        Driver driver = driverMapper.selectDriverByDriverId(driverId);
        if (driver != null) {
            if (1==driver.getEnableStatus()) {
                driver.setEnableStatus(0);
            } else {
                driver.setEnableStatus(1);
            }
            return ApiResult.success(driverMapper.updateDriver(driver));
        } else {
            return ApiResult.error("修改失败");
        }
    }

    @Override
    public ApiResult driverOrderNum(DriverOrderNumVo driverOrderNumVo) {
        Driver driver = driverMapper.selectDriverByDriverId(driverOrderNumVo.getDriverId());
        Integer orderNum =0;
        if(driver==null){
            return ApiResult.error("司机账号不存在");
        }
        Integer driverType = Optional.ofNullable(driver.getDriverType()).orElse(-1);
        if(driverType.equals(0)){//揽件员
            int count = orderService.count(new QueryWrapper<OrdOrder>().lambda()
                    .eq(OrdOrder::getCollectUserId, driverOrderNumVo.getDriverId())
                    .ge(OrdOrder::getDistributionStatus, "1"));
            orderNum=count;
        }
        if(driverType.equals(1)){//配送员
            int count = orderService.count(new QueryWrapper<OrdOrder>().lambda()
                    .eq(OrdOrder::getDeliveryUserId, driverOrderNumVo.getDriverId())
                    .eq(OrdOrder::getDistributionStatus, "5"));
            orderNum=count;
        }
        return ApiResult.success(orderNum);
    }

    @Override
    public List<DriverOrderVo> deliveryOrderPage(DriverOrderVo driverOrderVo) {
        List<DriverOrderVo> list=driverMapper.deliveryOrderPage(driverOrderVo);

        return list;
    }

    @Override
    public DriverDetailsVo getDriverDetailsByDriverId(Long driverId) {
        DriverDetailsVo driverDetailsVo = driverMapper.getDriverDetailsByDriverId(driverId);
        return driverDetailsVo;
    }

    @Override
    public ApiResult<String> createCode(ScanCodeParam scanCodeParam) {
        String qrCode = ScanCodeUtil.createQRCode(scanCodeParam.getOrderNo());
        return ApiResult.success(qrCode);
    }
    /**
     * 修改启用状态
     * @param param
     */
    @Override
    public void editEnable(EnableParam param) {
        Driver driver = driverMapper.selectById(param.getDriverId());
        AssertUtils.isNull(driver, "司机不存在");
        driver.setEnableStatus(param.getEnableStatus());
        driverMapper.updateById(driver);
    }
}
